package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.EvaluateMapper;
import com.example.education.model.Evaluate;
import com.example.education.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;

    @Override
    public boolean insertEvaluate(Evaluate evaluate) throws ParamException {
        if(evaluate==null){
            throw new ParamException("参数为空");
        }else if (evaluate.getContent()==null || "".equals(evaluate.getContent())){
            throw new ParamException("Content为空");
        }else if(evaluate.getCourseId()==null){
            throw new ParamException("CourseId为空");
        }else if (evaluate.getUserId()==null){
            throw new ParamException("UserId为空");
        }
        evaluate.setCreatTime(new Date());
        Integer result =  evaluateMapper.insertEvaluate(evaluate);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean likeEvaluate(Integer evaluateId) throws ParamException {
        if(evaluateId==null){
            throw new ParamException("参数为空");
        }
        Evaluate evaluate = getEvaluateById(evaluateId);
        Integer likeNum = evaluate.getLikeNum();
        Integer result = evaluateMapper.likeEvaluate(evaluateId,likeNum);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteEvaluate(Integer evaluateId) throws ParamException {
        if(evaluateId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  evaluateMapper.deleteEvaluate(evaluateId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean topEvaluate(Integer evaluateId) throws ParamException {
        if(evaluateId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  evaluateMapper.topEvaluate(evaluateId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean unTopEvaluate(Integer evaluateId) throws ParamException {
        if(evaluateId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  evaluateMapper.unTopEvaluate(evaluateId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Evaluate getEvaluateById(Integer evaluateId) throws ParamException {
        if(evaluateId==null){
            throw new ParamException("参数为空");
        }
        return evaluateMapper.getEvaluateById(evaluateId);
    }

    @Override
    public List<Evaluate> getEvaluateByCourseId(Integer courseId) throws ParamException {
        if(courseId==null){
            throw new ParamException("参数为空");
        }
        return evaluateMapper.getEvaluateByCourseId(courseId);
    }

    @Override
    public List<Evaluate> getAll(Integer page,Integer limit) throws ParamException {
        if (page == null) {
            throw new ParamException("page为空");
        } else if (limit == null) {
            throw new ParamException("limit为空");
        }
        return evaluateMapper.getAll((page - 1) * limit, limit);
    }

    @Override
    public int getAllCount(){
        return evaluateMapper.getAllCount();
    }
}
